// 蜂窝蜜造平台生成代码，如手工更改，请添加到 .beeignore 忽略生成
package com.fowo.api.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fowo.api.common.model.IdListPo;
import com.fowo.api.common.model.ImportRow;
import com.fowo.api.common.model.OptionItem;
import com.fowo.api.common.model.R;
import com.fowo.api.common.util.ResponseUtils;
import com.fowo.api.entity.SupplierFlowFormRelation;
import com.fowo.api.model.supplier.flow.form.relation.SupplierFlowFormRelationImportPo;
import com.fowo.api.model.supplier.flow.form.relation.SupplierFlowFormRelationItemVo;
import com.fowo.api.model.supplier.flow.form.relation.SupplierFlowFormRelationSearchParamPo;
import com.fowo.api.model.supplier.flow.form.relation.SupplierFlowFormRelationVo;
import com.fowo.api.service.SupplierFlowFormRelationService;
import com.fowo.api.sys.entity.SysFile;
import com.fowo.api.user.model.JwtUserInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import org.springframework.core.io.ClassPathResource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StreamUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@Tag(name = "供应商流程表单实例")
@RequestMapping("/api/supplier-flow-form-relation")
@RestController
public class SupplierFlowFormRelationController {

  @Resource
  private SupplierFlowFormRelationService service;

  @Operation(summary = "查询结果")
  @GetMapping
  public R<List<SupplierFlowFormRelationItemVo>> search(
    SupplierFlowFormRelationSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.search(search));
  }

  @Operation(summary = "快速查询选项(有缓存)")
  @GetMapping("options")
  public R<List<OptionItem<SupplierFlowFormRelationItemVo>>> searchOptions(
    SupplierFlowFormRelationSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.searchOptions(search));
  }

  @Operation(summary = "查询最后更新时间")
  @GetMapping("lastUpdateTime")
  public R<Date> searchLastUpdateTime(
    SupplierFlowFormRelationSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.searchLastUpdateTime(search));
  }

  @Operation(summary = "获取分页查询结果")
  @GetMapping("page")
  public R<Page<SupplierFlowFormRelationItemVo>> getPage(
    SupplierFlowFormRelationSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.pageSearch(search));
  }

  @Operation(summary = "获取包括子表的查询结果")
  @GetMapping("page-with-child")
  public R<Page<?>> getPageWithChild(
    SupplierFlowFormRelationSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.pageWithChildSearch(search));
  }

  @Operation(summary = "仅获取子表的查询结果")
  @GetMapping("page-only-child/{parentId}")
  public R<Page<?>> getPageOnlyChild(
    @PathVariable Long parentId,
    SupplierFlowFormRelationSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.pageOnlyChildSearch(parentId, search));
  }

  @Operation(summary = "查询是否后台下载")
  @GetMapping("getBackgroundExportFlag")
  public R<Boolean> getBackgroundExportFlag(
    SupplierFlowFormRelationSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.getBackgroundExportFlag(search));
  }

  @Operation(summary = "获取详情")
  @GetMapping("{id}")
  public R<SupplierFlowFormRelationVo> getVoById(@PathVariable Long id) {
    return R.data(() -> service.getVoById(id));
  }

  @Operation(summary = "获取表单id相关的编号列表")
  @GetMapping("form-id/{texts}/ids")
  public R<List<Long>> getIdsByFormId(@PathVariable String texts) {
    List<Long> nameList = Arrays
      .stream(texts.split(","))
      .map(Long::parseLong)
      .collect(Collectors.toList());
    return R.data(() -> service.getIdsByFormId(nameList));
  }

  @Operation(summary = "获取编号相关的表单id列表")
  @GetMapping("form-id/{ids}")
  public R<List<Long>> getVoById(@PathVariable String ids) {
    List<Long> idList = Arrays
      .asList(ids.split(","))
      .stream()
      .map(Long::valueOf)
      .collect(Collectors.toList());
    return R.data(() -> service.getFormIdByIds(idList));
  }

  @PreAuthorize("@am.canAccess('supplierFlowFormRelationCreate')")
  @Operation(summary = "新增")
  @PostMapping
  public R<Long> create(
    @Validated @RequestBody SupplierFlowFormRelationVo model
  ) {
    return R.data(() -> service.create(model));
  }

  @PreAuthorize("@am.canAccess('supplierFlowFormRelationEdit')")
  @Operation(summary = "更新")
  @PutMapping
  public R update(@RequestBody SupplierFlowFormRelationVo model) {
    return R.run(() -> service.update(model));
  }

  @PreAuthorize("@am.canAccess('supplierFlowFormRelationEdit')")
  @Operation(summary = "编辑更新")
  @PutMapping("edit")
  public R updateForEdit(@RequestBody SupplierFlowFormRelationVo model) {
    return R.run(() -> service.updateForEdit(model));
  }

  @PreAuthorize("@am.canAccess('supplierFlowFormRelationDelete')")
  @Operation(summary = "删除")
  @DeleteMapping("{id}")
  public R delete(@PathVariable Long id) {
    return R.run(() -> service.delete(id));
  }

  @PreAuthorize("@am.canAccess('supplierFlowFormRelationDelete')")
  @Operation(summary = "批量删除")
  @DeleteMapping("batch")
  public R batchDelete(@RequestBody IdListPo po) {
    return R.run(() -> service.batchDelete(po.getIds()));
  }

  @PreAuthorize("@am.canAccess('supplierFlowFormRelationImport')")
  @Operation(summary = "导入预览")
  @PostMapping("import-preview")
  public R<List<ImportRow<SupplierFlowFormRelationImportPo>>> importPreview(
    @RequestBody SysFile sysFile,
    @RequestParam(name = "template", required = false) Long templateId,
    @RequestParam(
      name = "allowOverrides",
      defaultValue = "false"
    ) boolean allowOverrides
  ) {
    return R.data(() ->
      service.importPreview(sysFile, templateId, allowOverrides)
    );
  }

  @PreAuthorize("@am.canAccess('supplierFlowFormRelationImport')")
  @Operation(summary = "完成导入")
  @PostMapping("import-done")
  public R importDone(
    @RequestBody List<ImportRow<SupplierFlowFormRelationImportPo>> rows,
    @RequestParam(name = "template", required = false) Long templateId
  ) {
    return R.run(() -> service.importDone(rows, templateId));
  }

  @Operation(summary = "下载导入模版")
  @GetMapping("import/template")
  public void downloadImportTemplate(HttpServletResponse response)
    throws IOException {
    ResponseUtils.setAttachmentFileName(
      response,
      "供应商流程表单实例导入模版.xlsx"
    );
    InputStream inputStream = new ClassPathResource(
      "templates/SupplierFlowFormRelation.xlsx"
    )
      .getInputStream();
    StreamUtils.copy(inputStream, response.getOutputStream());
  }

  @Operation(summary = "导出")
  @GetMapping("export")
  @PreAuthorize("@am.canAccess('supplierFlowFormRelationExport')")
  public void export(
    @RequestParam(name = "template", required = false) Long templateId,
    SupplierFlowFormRelationSearchParamPo search,
    HttpServletResponse response
  ) throws Exception {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    service.export(templateId, search, response);
  }

  /**
   * 视图选项卡标题上显示计数
   */
  @Operation(summary = "视图选项卡标题上显示计数")
  @GetMapping("getTabHeaderShowTotal")
  public R<Map> getTabHeaderShowTotal() {
    return R.data(() -> service.getTabHeaderShowTotal());
  }

  @Operation(summary = "全部")
  @GetMapping("all")
  public R<List<SupplierFlowFormRelationItemVo>> all(
    SupplierFlowFormRelationSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.all(search));
  }

  @Operation(summary = "全部分页")
  @GetMapping("allPage")
  public R<Page<SupplierFlowFormRelationItemVo>> allPage(
    SupplierFlowFormRelationSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.allPage(search));
  }

  @Operation(summary = "全部包括子表的查询结果")
  @GetMapping("allPage-with-child")
  public R<Page<?>> allPageWithChild(
    SupplierFlowFormRelationSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.allPageWithChildSearch(search));
  }

  @Operation(summary = "全部最后更新时间")
  @GetMapping("allLastUpdateTime")
  public R<Date> allLastUpdateTime(
    SupplierFlowFormRelationSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.allLastUpdateTime(search));
  }

  @Operation(summary = "待处理")
  @GetMapping("pending")
  public R<List<SupplierFlowFormRelationItemVo>> pending(
    SupplierFlowFormRelationSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.pending(search));
  }

  @Operation(summary = "待处理分页")
  @GetMapping("pendingPage")
  public R<Page<SupplierFlowFormRelationItemVo>> pendingPage(
    SupplierFlowFormRelationSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.pendingPage(search));
  }

  @Operation(summary = "待处理包括子表的查询结果")
  @GetMapping("pendingPage-with-child")
  public R<Page<?>> pendingPageWithChild(
    SupplierFlowFormRelationSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.pendingPageWithChildSearch(search));
  }

  @Operation(summary = "待处理最后更新时间")
  @GetMapping("pendingLastUpdateTime")
  public R<Date> pendingLastUpdateTime(
    SupplierFlowFormRelationSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.pendingLastUpdateTime(search));
  }

  @Operation(summary = "处理中")
  @GetMapping("processing")
  public R<List<SupplierFlowFormRelationItemVo>> processing(
    SupplierFlowFormRelationSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.processing(search));
  }

  @Operation(summary = "处理中分页")
  @GetMapping("processingPage")
  public R<Page<SupplierFlowFormRelationItemVo>> processingPage(
    SupplierFlowFormRelationSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.processingPage(search));
  }

  @Operation(summary = "处理中包括子表的查询结果")
  @GetMapping("processingPage-with-child")
  public R<Page<?>> processingPageWithChild(
    SupplierFlowFormRelationSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.processingPageWithChildSearch(search));
  }

  @Operation(summary = "处理中最后更新时间")
  @GetMapping("processingLastUpdateTime")
  public R<Date> processingLastUpdateTime(
    SupplierFlowFormRelationSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.processingLastUpdateTime(search));
  }

  @Operation(summary = "关于我")
  @GetMapping("about-me")
  public R<List<SupplierFlowFormRelationItemVo>> aboutMe(
    SupplierFlowFormRelationSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.aboutMe(search));
  }

  @Operation(summary = "关于我分页")
  @GetMapping("about-mePage")
  public R<Page<SupplierFlowFormRelationItemVo>> aboutMePage(
    SupplierFlowFormRelationSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.aboutMePage(search));
  }

  @Operation(summary = "关于我包括子表的查询结果")
  @GetMapping("about-mePage-with-child")
  public R<Page<?>> aboutMePageWithChild(
    SupplierFlowFormRelationSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.aboutMePageWithChildSearch(search));
  }

  @Operation(summary = "关于我最后更新时间")
  @GetMapping("about-meLastUpdateTime")
  public R<Date> aboutMeLastUpdateTime(
    SupplierFlowFormRelationSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.aboutMeLastUpdateTime(search));
  }

  @Operation(summary = "我发起")
  @GetMapping("start")
  public R<List<SupplierFlowFormRelationItemVo>> start(
    SupplierFlowFormRelationSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.start(search));
  }

  @Operation(summary = "我发起分页")
  @GetMapping("startPage")
  public R<Page<SupplierFlowFormRelationItemVo>> startPage(
    SupplierFlowFormRelationSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.startPage(search));
  }

  @Operation(summary = "我发起包括子表的查询结果")
  @GetMapping("startPage-with-child")
  public R<Page<?>> startPageWithChild(
    SupplierFlowFormRelationSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.startPageWithChildSearch(search));
  }

  @Operation(summary = "我发起最后更新时间")
  @GetMapping("startLastUpdateTime")
  public R<Date> startLastUpdateTime(
    SupplierFlowFormRelationSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.startLastUpdateTime(search));
  }

  @Operation(summary = "转发我")
  @GetMapping("forward-me")
  public R<List<SupplierFlowFormRelationItemVo>> forwardMe(
    SupplierFlowFormRelationSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.forwardMe(search));
  }

  @Operation(summary = "转发我分页")
  @GetMapping("forward-mePage")
  public R<Page<SupplierFlowFormRelationItemVo>> forwardMePage(
    SupplierFlowFormRelationSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.forwardMePage(search));
  }

  @Operation(summary = "转发我包括子表的查询结果")
  @GetMapping("forward-mePage-with-child")
  public R<Page<?>> forwardMePageWithChild(
    SupplierFlowFormRelationSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.forwardMePageWithChildSearch(search));
  }

  @Operation(summary = "转发我最后更新时间")
  @GetMapping("forward-meLastUpdateTime")
  public R<Date> forwardMeLastUpdateTime(
    SupplierFlowFormRelationSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.forwardMeLastUpdateTime(search));
  }
}
